package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingLevel> page = nursingLevelMapper.getBypage(name,status);
        PageResponse<NursingLevelVo>  pageResponse = PageResponse.of(page,NursingLevelVo.class);
        return pageResponse;

    }

    /**
     *  查询所有
     * @return
     */
    @Override
    public List<NursingLevelVo> getByAll() {
        List<NursingLevel> all = nursingLevelMapper.getByAll();
        if (all!=null){
            List<NursingLevelVo> nursingLevelVos = all.stream().map(nursingLevel -> {
                NursingLevelVo nursingLevelVo = new NursingLevelVo();
                BeanUtils.copyProperties(nursingLevel,nursingLevelVo);
                return nursingLevelVo;
            }).collect(Collectors.toList());
            return nursingLevelVos;
        }
        return null;
    }

    @Override
    public void insert(NursingLevelDto nursingLevelDto) {
             NursingLevel nursingLevel = new NursingLevel();
             BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
             nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        if (nursingLevel!=null){
            NursingLevelVo nursingLevelVo = new NursingLevelVo();
            BeanUtils.copyProperties(nursingLevel,nursingLevelVo);
            return nursingLevelVo;
        }
        return null;
    }

    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
        nursingLevelMapper.update(nursingLevel);

    }

    @Override
    public void delete(Long id) {
        nursingLevelMapper.delete(id);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingLevelMapper.enableOrDisable(id,status);
    }
}
